package com.maomao.maomaochong_i.chuangZhi;

import java.util.ArrayList;

/**
 * Created by Mrma on 17.5.19.
 */

public class GuoWu implements  Subject{
    private ArrayList<Xiugai> observer;
    private GuoWuShuJu guoCheShuJu;

    private static GuoWu guoWu;

    public static GuoWu getOne(){
        if (guoWu == null)
            guoWu = new GuoWu();
        return guoWu;
    }

    private GuoWu(){
        observer = new ArrayList<>();
    }
    @Override
    public void tianjia(Xiugai zhi) {
        observer.add(zhi);
    }

    @Override
    public void yichu(Xiugai o) {
        int i = observer.indexOf(o);
        if(i >= 0)
            observer.remove(i);
    }

    @Override
    public void tongzhi() {
        System.out.println(observer.size());
        for (int i = 0 ; i<observer.size();i++){
            Xiugai xiugai = observer.get(i);
            xiugai.xiugai(guoCheShuJu);
        }
    }

    public void setShu(GuoWuShuJu guoCheShuJu){
        this.guoCheShuJu = guoCheShuJu;
        tongzhi();
    }

}
